function wallActionMenu(source, callback){
	if($("#pagina").length != 0){
		$("#pagina").fadeOut("slow", function(){
			$("#pagina").remove();
			
			loadActionView(source, callback);
		});
	}
	else{
		loadActionView(source, callback);
	};
}

function loadActionView(source, callback){
	var innerContent = $("<div />", {
		id : "pagina",
	});
	
	innerContent.addClass("innerContent");
	innerContent.css("z-index", "198");
	innerContent.css("display", "none");
	innerContent.appendTo("#wall-data");
	
	innerContent.load(source, function(){
		innerContent.fadeIn("slow");
		callback.call(innerContent);
	});
}


$(document).ready(function() {
	if($("#content").length == 0){
		return;
	}
	
//	if($("#header").length != 0){
//		login.create("#header");
//	}
	
	$(document).ready(function(){
		if($("#content").length == 0){
			return;
		}
		
		var loginContainer = $("<div />", { id: "toppanel" });
		$("#content").before(loginContainer);
		loginContainer.load("parts/login.html #login-content", function(){
			login.create();
		});
	});
	
	$.get("/dotcao/ws/users/loggedin", function(data){
		if(data.firstname){
			$("#wall-menu #user-name").html(data.firstname + " " + data.lastname);
		}
		
		if(data.picture){
			$("#wall-menu #user-picture").attr("src", data.picture.path);
		}
	}, "json");
	
	$("#wall-more").bind("click", function() {
		window.location='menu.html';
		return false;
	});
	
	$("#wall-my-animals-command").bind("click", function(){
		wallActionMenu("show-animals.html .form-content", function(){
			loadUserAnimals("#pagina .form-content");											
			$("<button />", {id : "newAnimal", class: "button"}).html("Novo Animal").bind("click", function(){
				wallActionMenu("new/newanimal.html .form-content", function(){
					restoreAnimalScreen();
				});
			}).insertBefore("#pagina .form-content");
		});
	});

	$("#wall-available-animal").bind("click", function(){
		wallActionMenu("show-animals.html .form-content", function(){
			loadAvailableAnimals("#pagina .form-content");											
		});
	});

	$("#wall-profile-edit").bind("click", function(){
		window.location = "profile.html";
		return false;
	});
	
	$("#wall-news-command").bind("click", function(){
		wallActionMenu("new/newnews.html .form-content", function(){
			restoreNewsScreen();											
		});
	});

	$("#wall-event-command").bind("click", function(){
		wallActionMenu("new/newevent.html .form-content", function(){
			restoreEventScreen();											
		});
	});
	
	/* ***** MENU DEPOIMENTOS ***** */
	$("#wall-testimonials").bind("click", function() {
		if ($("#dial-testimony").length != 0) {
			return;
		}
		
		$("#wall-data").module.create({ title : "Depoimentos" }).live({
			name : "testimony",
			source : "parts/testimony.html .testimony",
			callback : function(data) {
				$(".testimony .content").html(data.content);
				$(".testimony .sender").html(data.sender.name);
				$(".testimony .comment").button();
				$(".testimony .picture").attr("src", data.sender.picture);
			}
		}).open("dial-testimony");
	});
	/* ***** MENU DEPOIMENTOS ***** */
	
	/* ***** MENU NOTICIAS ***** */
	$("#wall-news").bind("click", function() {
		if ($("#dial-news").length != 0) {
			return;
		}
		
		$("#wall-data").module.create({ title : "Últimas Notícias" }).live({
			name : "news",
			source : "parts/news.html .news",
			callback : function(data) {
				$(".news .content").html(data.content);
				$(".news .sender").html(data.sender.name);
				$(".body .title").html(data.title);
				$(".news .comment").button();
				$(".news .picture").attr("src", data.sender.picture);
			}
		}).open("dial-news");
	});
	/* ***** MENU NOTICIAS ***** */
});